<template>
  <div class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input
          class="input"
          type="text"
          placeholder="请输入用户名"
          v-model="account"
        />
      </div>
      <div class="error" v-if="accountError">
        <i class="iconfont icon-warning" />{{ accountError }}
      </div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input
          class="input"
          type="text"
          placeholder="请输入手机号"
          v-model="mobile"
          ref="mobileRef"
        />
      </div>
      <div class="error" v-if="mobileError">
        <i class="iconfont icon-warning" />{{ mobileError }}
      </div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input
          class="input"
          type="text"
          placeholder="请输入验证码"
          v-model="code"
        />
        <span class="code" @click="onSend">
          {{ time === 0 ? '发送验证码' : `${time}s后发送` }}
        </span>
      </div>
      <div class="error" v-if="codeError">
        <i class="iconfont icon-warning" />{{ codeError }}
      </div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input
          class="input"
          type="password"
          placeholder="请输入密码"
          v-model="password"
        />
      </div>
      <div class="error" v-if="passwordError">
        <i class="iconfont icon-warning" />{{ passwordError }}
      </div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input
          class="input"
          type="password"
          placeholder="请确认密码"
          v-model="rePassword"
        />
      </div>
      <div class="error" v-if="rePasswordError">
        <i class="iconfont icon-warning" />{{ rePasswordError }}
      </div>
    </div>
    <a href="javascript:;" class="submit" @click="onSubmit">立即提交</a>
  </div>
</template>

<script lang="ts" setup name="CallbackPatch">
import { useField, useForm } from 'vee-validate'
import useStore from '@/store'
import Message from '@/components/message/index.ts'
import { useRouter } from 'vue-router'
const { userStore } = useStore()
const router = useRouter()
import {
  accountRule,
  passwordRule,
  rePasswordRule,
  mobileRule,
  codeRule
} from '@/validate/rules'
import { ref } from 'vue'
import { useCountDown } from '@/utils/hooks'

let openId = ref('')
if (QC.Login.check()) {
  // 获取openId
  QC.Login.getMe((id) => {
    openId.value = id
  })
}

// 定义表单验证规则
const validationSchema = {
  account: accountRule,
  password: passwordRule,
  rePassword: rePasswordRule,
  mobile: mobileRule,
  code: codeRule
}
// 使用表单
const { validate } = useForm({
  validationSchema,
  // 设置初始值
  initialValues: {
    account: 'cdshi0006',
    password: '123456',
    mobile: '13111111111',
    code: '123456'
  }
})

// 使用字段
const { value: account, errorMessage: accountError } =
  useField<string>('account') // 获取 用户名 字段值和错误信息
const {
  value: mobile,
  errorMessage: mobileError,
  validate: validateMobile
} = useField<string>('mobile') // 获取 手机号 字段值和错误信息
const { value: code, errorMessage: codeError } = useField<boolean>('code') // 获取 验证码 字段值和错误信息
const { value: password, errorMessage: passwordError } =
  useField<string>('password') // 获取 密码 字段值和错误信息
const { value: rePassword, errorMessage: rePasswordError } =
  useField<string>('rePassword') // 获取 确认密码 字段值和错误信息

// 发送验证码
const mobileRef = ref<HTMLInputElement | null>(null)
const { time, start } = useCountDown(60)
const onSend = async () => {
  if (time.value > 0) return
  // 判断手机号是否验证通过
  const res = await validateMobile()
  if (!res.valid) {
    // 验证失败 获取焦点
    mobileRef.value!.focus()
    return
  }
  await userStore.sendQQPathMsg(mobile.value)
  // 开启倒计时
  start()
}

// 提交
const onSubmit = async () => {
  const res = await validate()
  if (res.valid) {
    await userStore.qqPatchLogin({
      openId: openId.value,
      mobile: mobile.value,
      code: code.value,
      account: account.value,
      password: password.value
    })
    Message.success('提交成功')
    router.push('/')
  }
}
</script>

<style scoped lang="less">
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
